package service.impl;

import dao.KnowledgeDao;
import dao.UserDao;
import dao.impl.KnowledgeDaoImpl;
import dao.impl.UserDaoImpl;
import exception.LoginException;
import pojo.Knowledge;
import pojo.User;
import pojo.vo.Message;
import service.UserService;
import util.EncryptSha256Util;

import java.io.PrintWriter;
import java.util.regex.Pattern;

/**
 * @author wq3stone
 */
public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    KnowledgeDao klgDao = new KnowledgeDaoImpl();
    @Override
    public Message login(String username, String password) {
        Message message = new Message();
        User user = userDao.query(username);
        if(user!=null){
            if(user.getPassword().equals(EncryptSha256Util.getSha256Str(password))){
                message.setCode(0);
                message.setMsg("登录成功");
                message.setObj(user);
            }else {
                message.setCode(1);
                message.setMsg("用户名或密码错误");
                message.setObj(null);
            }
        }else{
            message.setCode(2);
            message.setMsg("用户不存在");
            message.setObj(null);
        }
        return message;
    }

    @Override
    public Message register(String username, String password1, String password2) {
        Message message = new Message();
        if(userDao.query(username)!=null){
            message.setCode(1);
            message.setMsg("用户名已存在");
            message.setObj(null);
        }else {
            if (username.length()<3||username.length()>10){
                message.setCode(2);
                message.setMsg("用户名长度为3-10位");
                message.setObj(null);
            }else {
                if(!password1.equals(password2)){
                    message.setCode(3);
                    message.setMsg("两次密码输入不一致");
                    message.setObj(null);
                }else {
                    String rex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$";
                    if (password1.length()<8) {
                        message.setCode(4);
                        message.setMsg("密码至少为8位");
                        message.setObj(null);
                    }else if (!Pattern.matches(rex,password1)) {
                        message.setCode(5);
                        message.setMsg("密码由大小写字母和数字组成");
                        message.setObj(null);
                    }else {
                        userDao.insert(new User(username,password1));
                        message.setCode(0);
                        message.setMsg("注册成功");
                        message.setObj(userDao.query(username));
                    }
                }
            }
        }
        return message;
    }

    @Override
    public Message changePassword(String username, String origin, String now_password) {
        Message message = new Message();
        User user = userDao.query(username);
        if (EncryptSha256Util.getSha256Str(origin).equals(user.getPassword())) {
            if (origin.equals(now_password)) {
                message.setMsg("新密码不可与旧密码相同");
                message.setCode(-1);
            } else {
                user.setPassword(now_password);
                userDao.update(user);
                message.setMsg("密码修改成功！");
                message.setCode(0);
                message.setObj(user);
            }
        } else {
            message.setMsg("旧密码输入有误！");
            message.setCode(-1);
        }
        return message;
    }

}
